/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import wargame.dao.*;
import wargame.entity.*;
import wargame.entity.OutsourceProject;

/**
 *
 * @author Clarence
 */
public class ManageProject {

    public static boolean isProjectSubmitable(int project_id) {
        Project p = ProjectDAO.retrieveProject(project_id);

        boolean nonOutsourcedPortionCompleted = true;

        if (p.getMan_hours_c_sharp() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_dot_net() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_jsp() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_mssql() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_mysql() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_oracle() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_php() > 0) {
            nonOutsourcedPortionCompleted = false;
        }
        if (p.getMan_hours_ruby() > 0) {
            nonOutsourcedPortionCompleted = false;
        }

        if(p.getSubmitted() > 0){
            return false;
        }

        if (nonOutsourcedPortionCompleted == false) {
            return false;
        } else {
            ArrayList<OutsourceProject> op_list = OutsourceProjectDAO.retrieveAllOutsourceProjectOfProject(project_id);
            boolean submittable = true;

            for (int i = 0; i < op_list.size(); i++) {
                OutsourceProject op = op_list.get(i);
                if (op.getSubmitted() == 0) {
                    // not submittable
                    submittable = false;
                }

            }
            return submittable;
        }
    }

    public static boolean isOutsourcedProjectSubmitable(int os_project_id) {
        OutsourceProject op = OutsourceProjectDAO.retrieveOutsourceProject(os_project_id);
        boolean submitable = true;
        if (op.getMan_hours_c_sharp() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_dot_net() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_jsp() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_mssql() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_mysql() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_oracle() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_php() > 0) {
            submitable = false;
        }
        if (op.getMan_hours_ruby() > 0) {
            submitable = false;
        }

        if(op.getSubmitted() > 0){
            return false;
        }

        return submitable;
    }

    public static void submitProject(int project_id) {
        Project project = ProjectDAO.retrieveProject(project_id);

        try {

            if (ManageProject.isProjectSubmitable(project_id)) {

                Player player = PlayerDAO.retrievePlayer(project.getTaken_by());

                TransactionDAO tDAO = new TransactionDAO();
                Transaction newTransaction = new Transaction();

                newTransaction.setPlayer_id(player.getPlayer_id());
                newTransaction.setTurn(GameTurn.current_turn);
                newTransaction.setDescription("submit project:" + project.getProject_id());

                int penaltyAmt = 0;
                int balance = player.getBalance();
                int trackRecord = player.getTrack_record_points();

                //calculate the penalty amt if the project is submitted late
                boolean isOnTime = true;
                if (project.getDue_turn() < GameTurn.current_turn) {
                    isOnTime = false;

                    int penaltyTurns = GameTurn.current_turn - project.getDue_turn();
                    penaltyAmt = penaltyTurns * (1 / 10) * project.getBidded_price();
                }

                Analytic.createSubmitProjectAnalytic(project.getTaken_by(), isOnTime);

                //find the final balance of the player
                balance = balance + project.getBidded_price() - penaltyAmt;

                //adds track record or deduct depending on status
                if (penaltyAmt > 0) {
                    trackRecord -= project.getTrack_record_point();
                } else {
                    trackRecord += project.getTrack_record_point();
                }

                player.setBalance(balance);
                player.setTrack_record_points(trackRecord);

                //set debit amount
                newTransaction.setDebit_amount(project.getBidded_price() - penaltyAmt);

                //update player records
                PlayerDAO.updatePlayerRecords(player);

                //update project status
                project.setSubmitted(1);

                ProjectDAO.updateProjectStatus(project);

                //add transaction
                tDAO.insertTransaction(newTransaction);

                ManageEmployee.increaseMorale(project);

                //add into game log
                GameLog gl = new GameLog(player.getPlayer_id(), "Project: " + project.getProject_id() + ",submitted by: " + player.getPlayer_name(), GameTurn.current_turn);
                gl.setTimestamp(new Timestamp(new Date().getTime()));
                GameLogDAO.insertGameLog(gl);

            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    public static void submitOutsourcedProject(int os_project_id) {
        OutsourceProject op = OutsourceProjectDAO.retrieveOutsourceProject(os_project_id);
        if (ManageProject.isOutsourcedProjectSubmitable(os_project_id)) {
            op.setSubmitted(1);

            Player player_do_project = PlayerDAO.retrievePlayer(op.getTaken_by());
            Player player_offer_project = PlayerDAO.retrievePlayer(op.getCreator().getPlayer_id());

            int player_do_project_balance = player_do_project.getBalance();
            int player_offer_project_balance = player_offer_project.getBalance();

            if (op.getDue_turn() < GameTurn.current_turn) { //  Late
                player_do_project_balance += op.getBidded_price() * (1 - (GlobalConstant.PENALTY_PERCENTAGE * GameTurn.current_turn - op.getDue_turn()));
                player_offer_project_balance -= op.getBidded_price() * (1 - (GlobalConstant.PENALTY_PERCENTAGE * GameTurn.current_turn - op.getDue_turn()));
                Analytic.createSubmitOutsourceProjectAnalytic(player_do_project.getPlayer_id(), false);
            } else { // On Time
                player_do_project_balance += op.getBidded_price();
                player_offer_project_balance -= op.getBidded_price();
                Analytic.createSubmitOutsourceProjectAnalytic(player_do_project.getPlayer_id(), true);
            }

            ManageEmployee.increaseMorale(op);
            
            player_do_project.setBalance(player_do_project_balance);
            player_offer_project.setBalance(player_offer_project_balance);

            OutsourceProjectDAO.updateOutsourceProject(op);
            PlayerDAO.updatePlayer(player_do_project);
            PlayerDAO.updatePlayer(player_offer_project);

            TransactionDAO do_DAO = new TransactionDAO();
            TransactionDAO outsource_DAO=new TransactionDAO();

            Transaction player_do_Transaction = new Transaction();
            player_do_Transaction.setPlayer_id(player_do_project.getPlayer_id());
            player_do_Transaction.setTurn(GameTurn.current_turn);
            player_do_Transaction.setDescription("submitted Outsource project:" + os_project_id);
            player_do_Transaction.setDebit_amount(op.getBidded_price());
            
            do_DAO.insertTransaction(player_do_Transaction);

            Transaction player_Outsourcer_Transaction = new Transaction();
            player_Outsourcer_Transaction.setPlayer_id(player_offer_project.getPlayer_id());
            player_Outsourcer_Transaction.setTurn(GameTurn.current_turn);
            player_Outsourcer_Transaction.setDescription("paid for outsource project:" + os_project_id);
            player_Outsourcer_Transaction.setCredit_amount(op.getBidded_price());

            
            outsource_DAO.insertTransaction(player_Outsourcer_Transaction);
        }
    }
    // Projects only, does not include outsourced project
    // turn ++ must be called after this process, NOT BEFORE!
    /*
    public static void processOverDueProjects() {
    ArrayList<Project> project_list = ProjectDAO.retrieveAllProjects();

    for (int i = 0; i < project_list.size(); i++) {
    Project p = project_list.get(i);

    if (p.getSubmitted() == 0 && p.getTaken_by() > 0
    && p.getDue_turn() <= GameTurn.getTurnInfo().getCurrent_turn()) {
    // We are only interested in projects that are not submitted and taken by someone
    // and the project due turn must be the same turn as now or eariler
    Player player = PlayerDAO.retrievePlayer(p.getTaken_by());
    int penaltyAmount = ((int) (p.getBidded_price() * GlobalConstant.PENALTY_PERCENTAGE));
    player.setBalance(player.getBalance() - penaltyAmount);
    PlayerDAO.updatePlayer(player);
    }

    }
    }
     */
}

